package com.itheima.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        if (currentPage != 0 && page.size() == 0){
            currentPage = currentPage - 1;
            PageHelper.startPage(currentPage, pageSize);
            page = roleDao.selectByCondition(queryString);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Role role, Integer[] menus, Integer[] permissions) {
        //添加角色的基本信息,返回角色id
        roleDao.add(role);
        //获取角色id
        Integer roleId = role.getId();
        //根据角色的id关联对应的菜单信息
        setRoleAndMenu(roleId,menus);
        //添加角色和权限的对应关系
        setRoleAndpermissions(roleId,permissions);
    }

    //删除
    @Override
    public boolean deleteByid(Integer id) {
        //判断该角色是否有用户
          Integer count =  roleDao.queryById(id);

          if(count<=0){
              //无人有该角色,

              //清除权限与角色之间的关联
                roleDao.delete1(id);
              //清除菜单与角色之间的关联
                roleDao.delete2(id);
              //删除该角色
              roleDao.deleteRole(id);
              return true;
          }else {
                return false;
          }


       // roleDao.deleteByid(id);
    }

    /**
     * 提取的添加角色和菜单的对应关系
     * @param roleId
     * @param menus
     */
    public void setRoleAndMenu(Integer roleId, Integer[] menus) {
        if ( menus!= null && menus.length > 0) {
            for (Integer menuId : menus) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    /**
     * 提取的角色和权限的对应关系
     * @param menus
     * @param roleId
     * @param permissions
     */
    public void setRoleAndpermissions(Integer roleId, Integer[] permissions) {
        if ( permissions!= null && permissions.length > 0) {
            for (Integer permissionId : permissions) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", permissionId);
                roleDao.setRoleAndpermissions(map);
            }
        }
    }
}
